import os
import sys

from PySide6.QtGui import QAction
from PySide6.QtWidgets import (
    QStackedWidget,
    QVBoxLayout,
    QWidget,
    QApplication,
    QMainWindow,
)
from qfluentwidgets import setTheme, Theme

from app.utils.db import LocalCache
from app.view.EmulatorLaunchTool import EmulatorLaunchTool
from app.view.FloatToLatLon import FloatToLatLon
from app.view.LatLonToFloat2 import LatLonToFloat2
from app.view.PartyFeeCalculator import PartyFeeCalculator


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        setTheme(Theme.LIGHT)

        self.db = LocalCache()
        # self.init_window()
        # 创建一个堆叠窗口小部件
        self.stacked_widget = QStackedWidget()
        # 创建一个菜单栏
        menu_bar = self.menuBar()
        # 创建一个菜单
        view_menu = menu_bar.addMenu("菜单")
        menu_bar.addMenu("About")

        # 创建页面1和页面2
        page1 = LatLonToFloat2()
        page2 = FloatToLatLon()
        page3 = PartyFeeCalculator()
        page4 = EmulatorLaunchTool(self.db)

        # 将页面添加到堆叠窗口小部件中
        self.stacked_widget.addWidget(page1)
        self.stacked_widget.addWidget(page2)
        self.stacked_widget.addWidget(page3)
        self.stacked_widget.addWidget(page4)

        # 创建菜单项
        page1_action = QAction(page1.objectName(), self)
        page1_action.triggered.connect(lambda: self.stacked_widget.setCurrentIndex(0))
        view_menu.addAction(page1_action)

        page2_action = QAction(page2.objectName(), self)
        page2_action.triggered.connect(lambda: self.stacked_widget.setCurrentIndex(1))
        view_menu.addAction(page2_action)

        page3_action = QAction(page3.objectName(), self)
        page3_action.triggered.connect(lambda: self.stacked_widget.setCurrentIndex(2))
        view_menu.addAction(page3_action)

        page4_action = QAction(page4.objectName(), self)
        page4_action.triggered.connect(lambda: self.stacked_widget.setCurrentIndex(3))
        view_menu.addAction(page4_action)

        # 创建主窗口的布局
        layout = QVBoxLayout()
        layout.addWidget(self.stacked_widget)

        # 创建主窗口的小部件
        widget = QWidget()
        widget.setLayout(layout)

        # 设置主窗口的中央小部件
        self.setCentralWidget(widget)

    # def init_window(self):
    #     self.setWindowTitle("Tools")
    #     self.setWindowIcon(QIcon(":/resources/tools.png"))
    #     self.setMicaEffectEnabled(cfg.get(cfg.micaEnabled))
    #     # 设置窗口标志，禁用调节窗口大小
    #     self.setWindowFlags(
    #         self.windowFlags()
    #         | Qt.WindowType.WindowMaximizeButtonHint
    #         | Qt.WindowType.WindowMinimizeButtonHint
    #         | Qt.WindowType.WindowCloseButtonHint
    #         | Qt.WindowType.MSWindowsFixedSizeDialogHint)
    #
    #     # 设置窗口的大小和位置
    #     self.setGeometry(0, 0, 380, 280)
    #     # 获取屏幕信息
    #     screen = QApplication.primaryScreen()
    #     screen_geometry = screen.geometry()
    #
    #     # 计算窗口位置，使其居中于屏幕
    #     x = screen_geometry.center().x() - self.width() // 2
    #     y = screen_geometry.center().y() - self.height() // 2
    #     self.move(x, y)


if __name__ == "__main__":
    # 创建一个 PySide6 应用
    setTheme(Theme.LIGHT)
    app = QApplication([])
    # 启用 DPI 缩放
    # app.setAttribute(Qt.ApplicationAttribute.AA_UseHighDpiPixmaps)
    os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1"
    # 创建主窗口
    window = MainWindow()
    # 显示窗口
    window.show()
    # 运行应用
    sys.exit(app.exec())
